package ru.scan2d.main.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SettingsUtil
{
	public static final String OPEN_CHOOSER = "openChooser";
	public static final String SAVE_CHOOSER = "saveChooser";
	final static String fileName = System.getProperty("user.home")
			+ System.getProperty("file.separator") + "scan2D.properties";
	public static String getProperty(String propertyName) {
		try {
			
			final Properties properties = new Properties();
			properties.load(new FileInputStream(fileName));
			String userFolder = properties.getProperty(propertyName);
			return userFolder;
		} catch (FileNotFoundException e) {
			init();
			e.printStackTrace();
		} catch (IOException e) {
			init();
			e.printStackTrace();
		}
		return "";

	}

	public static void setProperty(String name, String value)
	{
		try {
		final Properties properties = new Properties();
		properties.load(new FileInputStream(fileName));
		properties.put(name, value);
		properties.store(new FileOutputStream(fileName), fileName);
		} catch (IOException e) {
			init();
			e.printStackTrace();
		}
	}
	
	public static void init(){
		try {
            final Properties properties = new Properties();
            properties.setProperty(OPEN_CHOOSER, "");
            properties.setProperty(SAVE_CHOOSER, "");
            properties.store(new FileOutputStream(fileName), fileName);
        } catch (Throwable t) {
            t.printStackTrace();
        }
	}
}
